//
//  ICMembersListController.m
//  ICome
//
//  Created by Administrator on 2016/12/29.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICMembersListController.h"
#import "ICMembersListlSearchController.h"
#import "ICContactListCell.h"
#import "UIImage+AvatarGeneration.h"

@interface ICMembersListController ()<UISearchControllerDelegate,UISearchBarDelegate,UITableViewDelegate, UITableViewDataSource, MembersListlSearchDelegate>

@property (nonatomic, strong) ICBaseSearchViewController *searchController;

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ICMembersListController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    for (int i = 0; i < self.users.count; i ++) {
        ICUser *user = self.users[i];
        if ([user.eId isEqualToString:[ICUser currentUser].eId]) {
            [self.users removeObjectAtIndex:i];
            break;
        }
    }
    [self setupUI];
}

- (void)loadData
{
    WEAKSELF
    [ICMessageDatabase getUsersWithGroupId:self.gId finish:^(NSArray *array, NSError *error) {
        if (array.count == 0) { // 数据异常
            [ICMessageHelper setGroupInfoLastUpdate:@0 gId:self.gId];
        }
        
        weakSelf.users   = (NSMutableArray *)array;
        
    }];
}



- (void)setupUI
{
    self.title = @"成员";
    ICMembersListlSearchController *searchVC   = [[ICMembersListlSearchController alloc] init];
    searchVC.delegate                    = self;
    searchVC.gId                         = self.gId;
    
    ICBaseSearchViewController *searchController = [[ICBaseSearchViewController alloc] initWithSearchResultsController:searchVC];
    searchController.placeholder = @"搜索";
    searchController.delegate = self;
    searchController.searchBar.delegate  = self;
    searchController.searchResultsUpdater = searchVC;
    self.definesPresentationContext = YES;
    _searchController = searchController;  // 强指针引用
    self.tableView.tableHeaderView = searchController.searchBar;
    
    self.tableView.frame  = CGRectMake(0,0, App_Frame_Width, APP_Frame_Height - 64);
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.users.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55.0f * Level;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *res = @"cell";
    ICContactListCell *cell = [tableView dequeueReusableCellWithIdentifier:res];
    if (!cell) {
        cell = [[ICContactListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:res];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    ICUser *model = [self.users objectAtIndex:indexPath.row];
    cell.titleLabel.text = model.eName;
    NSString *URL = [ICTools getMinImageURL:(model.photoId)];
    [UIImage addAvatariamgeView:cell.headImageView withUrlStr:URL witheId:model.eId withName:model.eName];
    return cell;
}
/** 加@字典*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ICUser *user = self.users[indexPath.row];
    NSLog(@"self.users =%@",self.users[indexPath.row]);
    NSString * name = [NSString stringWithFormat:@"%@\u2005",user.nName ==nil ?user.eName:user.nName];
    if ([self.delegate respondsToSelector:@selector(chooseMembers:)]) {
        [self.delegate chooseMembers:@{user.eId:name}];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
/** 加@字典*/
-(void)detailSearch:(NSDictionary *)dict
{
    if ([self.delegate respondsToSelector:@selector(chooseMembers:)]) {
        [self.delegate chooseMembers:dict];
    }
    [self.navigationController popViewControllerAnimated:YES];
}




#pragma mark - searchBarDelegate

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES];
    for(UIView *view in  [[[searchBar subviews] objectAtIndex:0] subviews]) {
        if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
            UIButton * cancel =(UIButton *)view;
            [cancel setTitle:@"取消" forState:UIControlStateNormal];
            [cancel setTitleColor:ICSEARCHCANCELCOLOR forState:UIControlStateNormal];
        }
    }
}



- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width, APP_Frame_Height-64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

@end
